package com.platform.boot.security.group;

import com.platform.boot.security.ReactiveSecurityUserHolder;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * @author <a href="https://github.com/vnobo">Alex bob</a>
 */
@Tag(name = "角色管理")
@RestController
@RequestMapping("/groups/v1")
@RequiredArgsConstructor
public class GroupsController {

    private final GroupsService managerService;

    @GetMapping("search")
    public Flux<Group> search(GroupRequest request, Pageable pageable) {
        return ReactiveSecurityUserHolder.getContext().flatMapMany(securityDetails ->
                this.managerService.search(request.securityCode(securityDetails.getTenantCode()), pageable));
    }

    @GetMapping("page")
    public Mono<Page<Group>> page(GroupRequest request, Pageable pageable) {
        return ReactiveSecurityUserHolder.getContext().flatMap(securityDetails ->
                this.managerService.page(request.securityCode(securityDetails.getTenantCode()), pageable));
    }

    @PostMapping("handle")
    public Mono<Group> handle(@Valid @RequestBody GroupRequest request) {
        return this.managerService.operate(request);
    }

    @DeleteMapping("delete")
    public Mono<Void> delete(@Valid @RequestBody GroupRequest request) {
        return this.managerService.delete(request);
    }


}